package br.senai.sc.web.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;

import br.senai.sc.controller.crud.UsuarioCRUD;
import br.senai.sc.model.Permissao;

/**
 * Faz a conversão de um objeto {@link Permissao} na tela.
 * 
 * @author Fernando Marquardt &lt;fernando.marquardt@gmail.com&gt;
 * @version $Id: PermissaoConverter.java 28 2012-03-07 20:48:20Z fernando.marquardt $
 */
@FacesConverter(forClass = Permissao.class)
public class PermissaoConverter implements Converter {

    @Inject
    private UsuarioCRUD usuarioCRUD;

    /**
     * {@inheritDoc}
     */
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        return usuarioCRUD.findPermissao(Integer.valueOf(value));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value instanceof Permissao) {
            return ((Permissao) value).getIdPermissao().toString();
        }

        return null;
    }
}